package cn.lutu.domain.repository.impl;

import cn.lutu.dao.mapper.ArticleCollectMapper;
import cn.lutu.dao.mapper.ArticleContentMapper;
import cn.lutu.dao.mapper.ArticleMapper;
import cn.lutu.dao.mapper.ArticleUpMapper;
import cn.lutu.domain.repository.ArticleRepository;
import cn.lutu.protocol.entity.Article;
import cn.lutu.protocol.entity.ArticleContent;
import cn.lutu.protocol.entity.Collect;
import cn.lutu.protocol.entity.Up;
import cn.lutu.protocol.search.ArticleSearchVO;
import cn.lutu.protocol.vo.*;
import cn.lutu.util.common.PageInfoToPageDataConverter;
import cn.lutu.util.pojo.vo.PageData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ArticleRepositoryImpl implements ArticleRepository {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleCollectMapper articleCollectMapper;

    @Autowired
    private ArticleUpMapper articleUpMapper;

    @Override
    public int insert(Article article) {
        log.debug("正在处理插入文章业务");
        return articleMapper.insert(article);
    }

    @Override
    public int insertContent(ArticleContent articleContent) {
        log.debug("正在处理插入文章内容业务");
        return articleContentMapper.insert(articleContent);
    }

    @Override
    public PageData<ArticleListVO> selectAll(Integer pageNum, Integer pageSize) {
        log.debug("正在处理获取全部文章列表");
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleListVO> list = articleMapper.selectAll();
        PageInfo<ArticleListVO> pageInfo = new PageInfo<>(list);
        PageData<ArticleListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public List<ArticleListVO> selectHotArt() {
        log.debug("正在处理获取热门文章列表");
        return articleMapper.selectHotArt();
    }

    @Override
    public List<ArticleListVO> selectRecent() {
        log.debug("正在处理获取最近文章列表");
        return articleMapper.selectRecent();
    }

    @Override
    public List<ArticleListVO> selectSearch(String textSearch) {
        log.debug("正在处理搜索文章列表");
        return articleMapper.selectSearch(textSearch);
    }

    @Override
    public List<HotListVO> selectHot() {
        log.debug("正在处理获取热门标题列表");
        return articleMapper.selectHot();
    }

    @Override
    public ArticleDetailVO selectArticleDetail(Long id) {
        log.debug("正在处理获取文章详情业务");
        return articleMapper.selectArticleDetail(id);
    }

    @Override
    public int insertArticle(Collect collect) {
        log.debug("正在处理插入收藏文章业务");
        return articleCollectMapper.insert(collect);
    }

    @Override
    public int deleteCollectById(Long userId, Long articleId) {
        log.debug("正在处理取消收藏文章业务");
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("article_id",articleId);
        return articleCollectMapper.delete(wrapper);
    }

    @Override
    public void updateCollect(Article article) {
        log.debug("正在处理文章收藏数业务,参数{}",article);
        articleMapper.updateById(article);
    }

    @Override
    public int insertUp(Up up) {
        log.debug("正在文章点赞业务,参数{}",up);
        return articleUpMapper.insert(up);
    }

    @Override
    public int deleteUpById(Long userId, Long articleId) {
        log.debug("正在处理取消点赞文章业务");
        QueryWrapper<Up> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("article_id",articleId);
        return articleUpMapper.delete(wrapper);
    }

    @Override
    public ArticleUpVO selectUp(Long userId,Long articleId) {
        log.debug("正在查询文章点赞业务,参数{}",userId,articleId);
        return articleMapper.selectUp(userId,articleId);
    }

    @Override
    public ArticleCollectVO selectCollect(Long userId, Long articleId) {
        log.debug("正在查询文章收藏业务,参数{}",userId,articleId);
        return articleMapper.selectCollect(userId,articleId);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("正在处理删除文章业务,参数{}",id);
        articleMapper.deleteById(id);
        QueryWrapper<ArticleContent> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id",id);
        articleContentMapper.delete(wrapper);

        QueryWrapper<Collect> wrapper1 = new QueryWrapper<>();
        wrapper.eq("article_id",id);
        articleCollectMapper.delete(wrapper1);

        QueryWrapper<Up> wrapper2 = new QueryWrapper<>();
        wrapper.eq("article_id",id);
        articleUpMapper.delete(wrapper2);
    }

    @Override
    public PageData<ArticleOthersListVO> selectByUserId(Integer pageNum, Integer pageSize,Long userId) {
        log.debug("正在处理获取作者其他文章业务,参数{}",userId);
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleOthersListVO> list = articleMapper.selectByUserId(userId);
        PageInfo<ArticleOthersListVO> pageInfo = new PageInfo<>(list);
        PageData<ArticleOthersListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<ArticleOthersListVO> selectArticleUp(Integer pageNum, Integer pageSize, Long userId) {
        log.debug("正在处理获取个人点赞的文章列表,参数{}",userId);
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleOthersListVO> list = articleMapper.selectArticleUp(userId);
        PageInfo<ArticleOthersListVO> pageInfo = new PageInfo<>(list);
        PageData<ArticleOthersListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<ArticleOthersListVO> selectArticleCollect(Integer pageNum, Integer pageSize,Long userId) {
        log.debug("正在处理获取个人收藏的文章列表,参数{}",userId);
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleOthersListVO> list = articleMapper.selectArticleCollect(userId);
        PageInfo<ArticleOthersListVO> pageInfo = new PageInfo<>(list);
        PageData<ArticleOthersListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<ArticleListVO> selectUnCheck(Integer pageNum, Integer pageSize) {
        log.debug("正在处理获取待审核文章列表");
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleListVO> list = articleMapper.selectUnCheck();
        PageInfo<ArticleListVO> pageInfo = new PageInfo<>(list);
        PageData<ArticleListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public int updateCheck(Article article) {
        log.debug("正在处理审核文章业务");
       return articleMapper.updateById(article);
    }

    @Override
    public int selectCount(Long articleId) {
        log.debug("正在处理统计文章业务");
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("id",articleId);
        return articleMapper.selectCount(wrapper);
    }

    @Override
    public List<ArticleListVO> rejectCheckList() {
        log.debug("正在处理获取审核不通过文章业务");
        return articleMapper.rejectCheckList();
    }

    @Override
    public List<ArticleListVO> rejectCheckListById(Long userId) {
        log.debug("正在处理获取个人审核不通过文章业务");
        return articleMapper.rejectCheckListById(userId);
    }

    @Override
    public PageData<ArticleSearchVO> listSearchVO(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询文章列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleSearchVO> list = articleMapper.listSearchVO();
        PageInfo<ArticleSearchVO> pageInfo = new PageInfo<>(list);
        PageData<ArticleSearchVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }


}
